package com.ss;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelXorXfermode;

//import android.util.Log;

class ChartIt extends View {
	
	private Paint  mPaint = new Paint();
	   
	   // private String s = "Using drawString!";
	    int length ;
	    int array_index = 0 ;
	    int max_rows_for_ticker= 0  ;
	    int index_for_prev_ticker=0  ;
	    int max_close ;
	    int curr_close ;
	   
	    int x_max = 310 ;
	    int x_min = 25 ;
	    int y_max = 400 ;
	    int y_min = 15;

	    int x_length = x_max -  x_min ;
	    int y_length = y_max -y_min  ;
	    int price_array[] ;
	    Line current_line ;
	    Line[] temp_line_array ;
	    
	    int  start_index_for_x_axis = 0 ;
	    	
   public ChartIt(Context arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}



   public ChartIt(Context context, Line[] line_array)
   {  
	  super(context);
	  mPaint.setColor(Color.RED);
	  mPaint.setStrokeWidth(2);
	  mPaint.setXfermode(new PixelXorXfermode(Color.WHITE));
	  mPaint.setAntiAlias(true);
	  mPaint.setSubpixelText(true);

      length = line_array.length ;


      price_array = new int [ length ] ;
      temp_line_array = line_array ;
      while ( array_index < length )
      {
       current_line = line_array[array_index] ;
       if (current_line == null || current_line.getTicker() == null )
                break ;

       try {
          if ( current_line.getClose() != null )
              curr_close =
               (int ) Double.parseDouble( current_line.getClose() ) ;
          else
                break ;     // Break out of the loop.
           }
       catch ( NumberFormatException nfe )
          {
             System.out.println( nfe.toString() ) ;
          }
       price_array[array_index] = curr_close ;
       if ( curr_close > max_close )
       {
        max_close = curr_close ;
       }
        
       if ( ( line_array[array_index+1] == null )  ||
           ( ( line_array[array_index+1] != null ) &&
            ( current_line.getTicker()
              != line_array[array_index+1].getTicker() )) )
       {  // Next Ticker

       //System.out.println("prev_ticker :" +  current_line.getTicker() ) ;

           if ( max_rows_for_ticker < (array_index - index_for_prev_ticker) )
           {
                 max_rows_for_ticker = array_index - index_for_prev_ticker ;
                 if (  index_for_prev_ticker  != 0 )
                     start_index_for_x_axis = index_for_prev_ticker + 1   ;

           }
           index_for_prev_ticker  = array_index  ;
       }


      
      array_index++ ;
      }      // End of while


      
   }


       @Override protected void onDraw(Canvas canvas) {
    	   int count = 0 ;
    	   int prev_x = x_min ;
    	   int prev_y = y_max ;
    	   int curr_x =0 ;
    	   int curr_y = 0 ;
    	   int paint_array_count = 0 ;
    	   int done = 0 ;
    	   int ticker_start_index = 0 ;
    	   String x_display="";
    	   
            canvas.drawColor(Color.WHITE);
 
            
            canvas.drawLine(x_min, y_min, x_min, y_max, mPaint);
            canvas.drawLine(x_min, y_max, x_max, y_max, mPaint);
            //          Add Values to Y-AXIS
            curr_x = x_min ;
            for (count = ( max_close/10 ) ; count < max_close ; count= count+( max_close/ 10 )  )
            {
            curr_y = y_max - (( count * y_length ) / max_close ) ;

            canvas.drawText( "__" , curr_x-5 , curr_y, mPaint );
            canvas.drawText(  String.valueOf( count )  , curr_x-20 , curr_y , mPaint);

            }
            
            // Add Values on X-AXIS 
            curr_y = y_max ;
            paint_array_count = 0 ;
            for (count = 0 ; count < 5 ; count++ )
            {
          
		            curr_x =   x_max - ((x_length / 5) * count ) ;
		              paint_array_count  = start_index_for_x_axis +
		                                   ( count *  max_rows_for_ticker / 5 ) ;
		                                  
		           // System.out.println("curr_x: "  + curr_x
		           //                   +" curr_y:" +  curr_y ) ;
		
		            
		            canvas.drawText( "|" , curr_x , curr_y, mPaint );
		            x_display=temp_line_array[paint_array_count].getDate();
		            // Extract YYYY-MM Value
		            x_display=x_display.substring(0,7);
		            canvas.drawText(  x_display  , curr_x-35 , curr_y+15, mPaint );

            }

            // g.setColor( Color.red );
            
            while ( done == 0   )
            {
            // Draw the Graph
            prev_x = x_min ;
            prev_y = y_max ;

            for (count = 0 ; count < array_index ; count++ )
            {
            curr_x = x_max - ( count * x_length / max_rows_for_ticker ) ;
            curr_y = y_max - (price_array[ count+ticker_start_index ] * y_length / max_close ) ;
          //  System.out.println("curr_x: "  + curr_x
          //                    +" curr_y:" +  curr_y ) ;

            if ( count > 0 )
            {
              canvas.drawLine( prev_x, prev_y , curr_x , curr_y, mPaint );
              if ( count+ticker_start_index+1 == array_index )
              {
                  done = 1 ;
                  break ;
              }
              if (  temp_line_array[count+ticker_start_index].getTicker() !=
                    temp_line_array[count+ticker_start_index+1].getTicker() )
              {  // Next Ticker
                     
                      ticker_start_index += count+1 ;
                   //   System.out.println("Changing color "  + ticker_start_index ) ;
                   /*    Will be needed for multiple tickers
                      g.setColor(
                               new Color ((int) (Math.random() * 256) ,
                                          (int) (Math.random() * 256) ,
                                          (int) (Math.random() * 256) 
                               )
                        );
                   */ 
                        break ;

              }
            }
            else if ( count == 0 )
             canvas.drawText( temp_line_array[count+ticker_start_index].getTicker() , curr_x-20 , curr_y-10, mPaint );

            prev_x = curr_x ;
            prev_y = curr_y ;
            }   // End For
           
            canvas.drawText( String.valueOf( max_close)  ,
                        x_min-5 , y_max - (max_close * y_length / max_close ), mPaint );

            } // End While

            
        } // End OnDraw
 
} // End of class chartit.
